package org.carl.base.driving.config;

import com.pugwoo.dbhelper.DBHelper;
import com.pugwoo.dbhelper.impl.SpringJdbcDBHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;

import javax.sql.DataSource;

@Configuration
@Slf4j
public class JdbcConfig {
    @Autowired
    private DataSource dataSource;

    @Bean
    @ConditionalOnBean(value = DataSource.class)
    public JdbcTemplate jdbcTemplate() {
        return new JdbcTemplate(dataSource);
    }

    @Bean
    @ConditionalOnBean(value = DataSource.class)
    public NamedParameterJdbcTemplate namedParameterJdbcTemplate() {
        return new NamedParameterJdbcTemplate(dataSource);
    }

    @Bean
    public DBHelper dbHelper() {
        SpringJdbcDBHelper springJdbcDBHelper = new SpringJdbcDBHelper();
        springJdbcDBHelper.setJdbcTemplate(jdbcTemplate());
        springJdbcDBHelper.setNamedParameterJdbcTemplate(namedParameterJdbcTemplate());
        return springJdbcDBHelper;
    }
}
